<template> 
  <div class="app-container" v-loading="listLoading">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>添加用户</span>
        <div class='userForm'>
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="ruleForm.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="username">
              <el-input  v-model="ruleForm.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input type="text" v-model.number="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="note">
              <el-input type="textarea" v-model="ruleForm.note"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>



  </div>
</template>
<script>
  import {registerUser} from '@/api/user'
import { Message, MessageBox } from 'element-ui'
  export default {
    data() {
      var email = (rule, value, callback) => {
        console.log('email',value,rule)
        if (value === '') {
          callback(new Error('请输入邮箱'));
        }
        else if (value.length <= 20 && value.length>=6) {
          let reg =  /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
          if (reg.test(value)) {
            callback();
          } else {
            callback(new Error('请输入正确的邮箱'));
          }
        } else {
          callback(new Error('邮箱长度在 2 到 20 个字符'));
        }
      };
      var username = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else if (value.length <= 20 && value.length>=2) {
          let reg =  /^[a-zA-Z\u4e00-\u9fa5]+$/
          if (reg.test(value)) {
            callback();
          } else {
            callback(new Error('请输入正确的用户名'));
          }
        } else {
          callback(new Error('用户名长度在 2 到 20 个字符'));
        }
      };
      var password = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if (value.length <= 20 && value.length>=6) {
          callback();
        } else {
           callback(new Error('密码长度在 6 到 20 个字符'));
        }
      };
      var note = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入备注'));
        } else if (value.length <= 20 && value.length>=6) {
          callback();
        } else {
           callback(new Error('备注长度在 2 到 50 个字符'));
        }
      };



      return {
        listLoading: false,

        userName: '',

        ruleForm: {
          password: 'spdb123213',
          username: 'qwewqe',
          email: 'sdasd@qq.com',
          note: 'dsadsadsad'
        },
        rules: {

          username: [   // 用户名
            { validator: username, trigger: 'blur' }
          ],
          note: [   //  备注
            { validator: note, trigger: 'blur'}
          ],
          email: [   //  邮箱
            { validator: email, trigger: 'blur',  }
          ],
          password: [    //  密码
            { validator: password, trigger: 'blur'}
          ],
        }
      };
    },
    methods: {
      addUser() {
        registerUser(this.ruleForm).then(response => {
          this.listLoading = false;
          if (response.code == 500) {
            Message({
                message: '管理员名称重复',
                type: 'fail',
                duration: 3 * 1000
              })
          } else if( response.code == 200 ) {
              Message({
                message: '注册成功',
                type: 'success',
                duration: 3 * 1000
              })
              this.$router.push({
                path:'/user/list'
              })
          }
        });
      },



      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.listLoading = true
            this.addUser()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scoped>
  .input-width {
    width: 203px;
  }
  .userForm{
    width: 600px;
    margin-top: 50px;
    margin-left: 100px;
  }
</style>


